\ Preprocessor closures demo - Copyright 2012 J.L. Bezemer
\ You can redistribute this file and/or modify it under
\ the terms of the GNU General Public License

[pragma] forcecellheap
include lib/ansmem.4th
include 4pp/lib/closures.4pp
                                       \ counter creation word
:: counter                             ( n1 n2 addr --)
   fields                              \ define closure data
     field: start                      \ initial value
     field: inc                        \ increment per call
   end-fields                          \ only cells may be used!

   >r r@ -> inc ! r@ -> start !        \ initialise closure
   :noname dup -> inc @  swap -> start dup ? +! ; r> !
;                                      \ assign default behavior by directly
                                       \ storing the quotation in "addr"

5 2 new counter counter1               \ make a counter in dynamic memory

counter1                               \ 5
counter1                               \ 7
counter1                               \ 9

13 3 static counter counter2           \ make a counter in static memory

counter2                               \ 13
counter2                               \ 16
counter2                               \ 19
